<html>
<head><meta charset="utf-8"><title>Transmuting with type parameters · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20with.20type.20parameters.html">Transmuting with type parameters</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="242929374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20with%20type%20parameters/near/242929374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Wagner-Hall <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20with.20type.20parameters.html#242929374">(Jun 16 2021 at 19:21)</a>:</h4>
<p>Howdy! I put together <a href="https://github.com/rust-lang/rust/pull/86281">https://github.com/rust-lang/rust/pull/86281</a> to allow limited transmuting between types which are otherwise reasonable to transmute except they happen to reference type parameters (which haven't yet been resolved because we're pre-monomorphisation).</p>
<p>I'm not super confident with transmute-safety in general, so I'd love to borrow anyone more knowledgable's eyes to make sure it's not opening up horrible mistakes!</p>



<a name="242967743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20with%20type%20parameters/near/242967743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20with.20type.20parameters.html#242967743">(Jun 17 2021 at 03:04)</a>:</h4>
<p>Given that this is an extension to <code>transmute</code>, which is already unsafe, I don't think there is much to worry about other than the possibility of post-monomorphization errors if this is not done correctly. As far as I know, the two properties listed (a <code>repr(transparent)</code> type has the same size as its non-ZST member, and <code>[T; N]</code> and <code>[U; N]</code> have the same size if <code>T</code> and <code>U</code> do) hold unconditionally, and these are documented in the UCG document, except that <a href="https://rust-lang.github.io/unsafe-code-guidelines/layout/arrays-and-slices.html#special-case-stride--size"><code>size == stride</code></a> is still open. With these two clauses alone, though, this is not a problem, because <code>repr(transparent)</code> yields a type with the same size and stride, and same-length arrays of types with the same size and stride also have the same size and stride.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>